/* eslint-disable  */
import { storage, getter } from '@/utils';
import { TOKENKEY } from '@/common/const';
import qs from 'qs'

import { history } from 'umi';
import { Role, TaskCodeCustomer } from './const';
import { Toast } from 'antd-mobile';

const commonMenu = [
  {
    key: 'home',
    icon: 'icon-ic_home_home_dark',
    selectedIcon: 'icon-ic_home_home_light',
  },
  {
    key: 'component',
    icon: 'icon-list_edit',
    selectedIcon: 'icon-icon_edit',
  },
  {
    key: 'expand',
    icon: 'icon-icon_score_no',
    selectedIcon: 'icon-icon_score',
  },
  {
    key: 'my',
    icon: 'icon-ic_home_home_dark',
    selectedIcon: 'icon-ic_home_home_light',
  },
];

/**
 *接口响应枚举
 */
enum HttpCode {
  SUCCESS = 0, // 成功
  FAIL = 1, // 失败
  WARN = 2, // 警告
  NO_LOGIN = 1000, // 需要登录
}

/**
 * 权限状态
 */
enum AuthorityStatus {
  USER = 1,
  ADMIN = 2,
  CLOSE = 3,
  GUEST = 0,
}

export { commonMenu, HttpCode, AuthorityStatus };

type UserInfo = {
  // 登录用户角色
  role: typeof Role['主借人'];
  mobile: string | number;
  token?: string;
  orderNo?: string;
};

export { Role };

/**
 * 获取登录角色信息
 */
export const getUerInfo = () => {
  const data = storage.get('hr-user');
  if (data) {
    return JSON.parse(data) as UserInfo;
  }
  return false;
};

/**
 * 设置登录角色信息
 */
export const setUerInfo = (data: UserInfo) => storage.set('hr-user', JSON.stringify(data));

// 跳转电子签
export const goEsign = async ({ taskCode, orderNo,taskId }, payload:any = {}) => {
  const next = TaskCodeCustomer[taskCode];

 

  if (next.path) {
    if(payload.realTaskCode === '5'){
      taskCode = '5'
    }
    console.log(`${next.path}?waterNo=${orderNo}&successUrl=${encodeURIComponent(successUrl)}&failUrl=${encodeURIComponent(cancelUrl)}`, '跳转电子签');
    window.location.href = `${next.path}?orderNo=${orderNo}&successUrl=${encodeURIComponent(successUrl)}&failUrl=${encodeURIComponent(cancelUrl)}`;
  }
};

/**
 * 任务的下个节点跳转
 * @param param0
 * @param payload
 *
 */
export const taskNext = (param0, payload: any = {}) => {
  const { taskCode = '', orderNo = '',taskId=''} = param0
  const next = TaskCodeCustomer[taskCode] || {};
  let { url, others } = payload;
  // // 信息录入
  // if(taskCode ==='2'){
  //   return history.push(`/order/add/borrower?orderNo=${orderNo}`);
  // }
  // 如果是电子签的节点
  if (taskCode === '3') {
    return goEsign({ taskCode, orderNo,taskId }, others);
  }
  if (taskCode === '4'){ //审批中按钮无法点击
    return
  }
  // 存在下一个节点
  if (next.path) {
    if(next.params){
      let obj = {}
      next.params.forEach(item => {
        obj[item] = param0[item]
      })
      url = next.path + '?' + qs.stringify(obj)
      return history.push(url)
    }
    return history.push(`${next.path}?orderNo=${orderNo}&now=${Date.now()}`);
  }
  // 默认成功页面
  return history.push(url || '/order/add/result');
};

/**
 * 任务的上一个节点跳转
 */
export const taskPre = async ({ taskId, orderNo = '' }) => {
  history.goBack();
};
